#!/usr/bin/env bash
# set -x
set -euo pipefail


function state_set() {
    STATE=$1

    tmp=$(mktemp)
    jq --exit-status --arg value "$STATE" '.state = ($value)' $DEPLOY_ROOT/status.json > "$tmp" 
    mv "$tmp" $DEPLOY_ROOT/status.json
}

function state_get() {
    status=$(cat $DEPLOY_ROOT/status.json)
	state=$(echo $status | jq -r --exit-status '.state')
    echo $state
}

function module_set() {
    MODULE=$1

    tmp=$(mktemp)
    jq --exit-status --arg value "$MODULE" '.module = ($value)' $DEPLOY_ROOT/status.json > "$tmp" 
    mv "$tmp" $DEPLOY_ROOT/status.json
}

function module_get() {
    status=$(cat $DEPLOY_ROOT/status.json)
    module=$(echo $status | jq -r --exit-status '.module')
    echo $module
}

function recover_set() {
    MODULE=$1

    tmp=$(mktemp)
    jq --exit-status --arg value "$MODULE" '.recovery = ($value)' $DEPLOY_ROOT/status.json > "$tmp" 
    mv "$tmp" $DEPLOY_ROOT/status.json
}

function recover_get() {
    status=$(cat $DEPLOY_ROOT/status.json)
    module=$(echo $status | jq -r --exit-status '.recovery')
    echo $module
}